﻿using Powerless_View.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;

namespace Powerless_View.Controllers.Actions
{
    public class GetReportAndModelsActionResult : ActionResult
    {
        private string _reportPath;

        public GetReportAndModelsActionResult(string reportPath) : base()
        {
            _reportPath = reportPath;
        }

        public override void ExecuteResult(System.Web.Mvc.ControllerContext context)
        {
            //Get report
            var getter = new HttpGetter();
            var reportStream = getter.GetResponse(this._reportPath);

            //Create datasources xml
            var datasources = new StringBuilder();
            datasources.Append("<?xml version=\"1.0\"?>");
            datasources.Append("<ArrayOfDataSource xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://schemas.microsoft.com/sqlserver/reporting/2010/03/01/ReportServer\">");
            datasources.Append("  <DataSource>");
            datasources.Append("    <Name>EntityDataSource</Name>");
            datasources.Append("    <DataSourceDefinition>");
            datasources.Append("      <Extension>DAX</Extension>");
            datasources.Append("      <ConnectString>Data Source=\"http://localhost/PowerPivot Gallery/ReportSource.xlsx\";</ConnectString>");
            datasources.Append("      <UseOriginalConnectString>false</UseOriginalConnectString>");
            datasources.Append("      <OriginalConnectStringExpressionBased>false</OriginalConnectStringExpressionBased>");
            datasources.Append("      <CredentialRetrieval>Integrated</CredentialRetrieval>");
            datasources.Append("      <WindowsCredentials>false</WindowsCredentials>");
            datasources.Append("      <ImpersonateUser>false</ImpersonateUser>");
            datasources.Append("      <Prompt>Enter a user name and password to access the data source:</Prompt>");
            datasources.Append("      <Enabled>true</Enabled>");
            datasources.Append("    </DataSourceDefinition>");
            datasources.Append("  </DataSource>");
            datasources.Append("</ArrayOfDataSource>");

            //convert data sources to byte array
            var responseArray = reportStream.ToArray();
            responseArray.Concat(GetBytes(datasources.ToString()));

            context.HttpContext.Response.ContentType = "application/progressive-report";
            context.HttpContext.Response.BinaryWrite(responseArray);
        }

        /// <summary>
        /// http://stackoverflow.com/questions/472906/net-string-to-byte-array-c-sharp
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private byte[] GetBytes(string str)
        {
            byte[] bytes = new byte[str.Length * sizeof(char)];
            System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
            return bytes;
        }
    }
}